package com.niiwoo.civet.trade.service.local.newRefund.notify.mq;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.niiwoo.activity.stage.enums.prize.UserPrizeUseSceneEnum;
import com.niiwoo.civet.base.enums.RefundTypeEnum;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestOrder;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestorPlan;
import com.niiwoo.civet.trade.dao.entity.RefundNotifyRecord;
import com.niiwoo.civet.trade.dao.entity.RefundOrder;
import com.niiwoo.civet.trade.dao.entity.RefundOrderDetailBiz;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestOrderMapper;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestorPlanMapper;
import com.niiwoo.civet.trade.dto.common.InvestReturnAmountMsgDTO;
import com.niiwoo.civet.trade.dto.common.InvestReturnAmountMsgDTO.InvestReturnAmountPlanDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.tripod.lanmao.enums.BusinessTypeEnum;

/**
 * 活动中心-投资回款MQ通知
 * 
 * 触发条件：用户还款、担保公司垫付
 *
 * @author: kevin.jia
 * @version 1.0.0
 */
@Component
public class ActivityEventInvestReturnAmountNotify extends MQRefundNotify {
	
	@Autowired
	private ProjectInvestorPlanMapper projectInvestorPlanMapper;
	
	@Autowired
	private ProjectInvestOrderMapper projectInvestOrderMapper;
	
	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		RefundOrder refundOrder = refundNotifyDTO.getRefundOrder();
		RefundNotifyRecord refundNotifyRecord = refundNotifyDTO.getRefundNotifyRecord();
		List<RefundOrderDetailBiz> refundOrderDetailBizList = refundNotifyDTO.getRefundOrderDetailBizList();
		
    	ProjectInvestOrder projectInvestOrder = projectInvestOrderMapper.selectByPrimaryKey(refundNotifyRecord.getInvestOrderId());
		
		InvestReturnAmountMsgDTO msgDTO = new InvestReturnAmountMsgDTO();
		msgDTO.setInvestOrderId(refundNotifyRecord.getInvestOrderId());
		msgDTO.setProjectId(projectInvestOrder.getProjectId());
		msgDTO.setUserId(projectInvestOrder.getUserId());
		if (RefundTypeEnum.OVERDEPT.getRefundType().equals(refundOrder.getRefundType())) {
			msgDTO.setUserPrizeUseScene(UserPrizeUseSceneEnum.REPAYMENT_ADVANCE.getType());
		}
		
		// 回款计划详情		
		List<InvestReturnAmountPlanDTO> investReturnAmountPlanDTOList = Lists.newArrayList();
		msgDTO.setInvestReturnAmountPlanDTOList(investReturnAmountPlanDTOList);
		
		for (RefundOrderDetailBiz refundOrderDetailBiz : refundOrderDetailBizList) {
			// 回款金额小于0、非还款，代偿的直接跳过
			if (refundOrderDetailBiz.getAmount().compareTo(BigDecimal.ZERO) <= 0 ||
					(!BusinessTypeEnum.REPAYMENT.name().equals(refundOrderDetailBiz.getBizType()) && !BusinessTypeEnum.COMPENSATORY.name().equals(refundOrderDetailBiz.getBizType()))) {
				continue;
			}
			
			// 按期数拆分
			ProjectInvestorPlan projectInvestorPlan = projectInvestorPlanMapper.selectByPrimaryKey(refundOrderDetailBiz.getBackPlanId());			
			if (null != projectInvestorPlan && refundNotifyRecord.getInvestOrderId().equals(projectInvestorPlan.getInvestOrderId())) {
				InvestReturnAmountPlanDTO investReturnAmountPlanDTO = new InvestReturnAmountMsgDTO.InvestReturnAmountPlanDTO();
				investReturnAmountPlanDTO.setCapital(refundOrderDetailBiz.getCapital());
				investReturnAmountPlanDTO.setInterest(refundOrderDetailBiz.getInterest());
				investReturnAmountPlanDTO.setCurrentPeriod(refundOrderDetailBiz.getCurrentPeriod());
				investReturnAmountPlanDTOList.add(investReturnAmountPlanDTO);
			}
		}
		
		// 该投资订单没有回款，返回NULL
		if (CollectionUtils.isEmpty(investReturnAmountPlanDTOList)) {
			return null;
		}
		return msgDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ACTIVITY_EVENT_INVEST_RETURN_AMOUNT;
	}
}